package com.ws.convert;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateConvert implements Converter<String, Date> {
    @SneakyThrows
    @Override
    public Date convert(String source) {
        System.out.println(source);
        SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat f2 = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat f3 = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            if(source.contains("-")) {
                return f1.parse(source);
            }else if(source.contains("/")) {
                return f2.parse(source);
            }else if (source.contains("年")) {
                return f3.parse(source);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
